import concurrent
import json

from tools.common import *
from tools.get_headerjson import GetcJson
from conf.api_path import ApiPath
from tools.get_headerjson import GetHeaderjson


import requests
from concurrent.futures import ThreadPoolExecutor
from excute import get_imei

# 定义API地址
API_URL = "http://172.16.1.29/open-api/device/operate/audioStop"

ApiPath = ApiPath()
GetcJson = GetcJson()
GetHeaderjson = GetHeaderjson()

"""
# 不同请求参数的列表（示例）
params_list = [
  {"param1": "value1", "param2": 100},
  {"param1": "value2", "param2": 200},
  {"param1": "value3", "param2": 300}
]
"""
tup,len,id = get_imei()
params_list = []

for i in range(len):
  for imei in id[i]:
    #topic = f"IOT/intranet/server/report/prod/{imei}"
    deviceId = id[i][0]
    params = {"requestData": {"deviceId": deviceId}}
    params_json = json.dumps(params)
    #params = '{"requestData": {"deviceId": {deviceId}},"page": {"page": 1, "pageSize": 10, "orderByColumn": "", "isAsc": "desc", "reasonable": "true","total": 0}}'
    params_list.append(params_json)





def make_request(params):
  try:
    headers = GetHeaderjson.get_s_headers()
    body = json.loads(params)
    response = requests.post(API_URL, params=body,headers=headers,timeout=5)
    response.raise_for_status()  # 检查HTTP错误
    return  response.json()
    #print("打印接口请求返回：",response.json())
  except Exception as e:
    print(f"请求失败 {params}: {str(e)}")
    return None



# 使用线程池并发请求
with ThreadPoolExecutor(max_workers=10) as executor:  # 控制并发数量
  # 提交所有任务

  print("打印出所有的请求参数",params_list)
  future_to_params = {executor.submit(make_request, params): params for params in params_list}

  # 处理结果(回调可自定义)
  for future in concurrent.futures.as_completed(future_to_params):
    params = future_to_params[future]
    try:
      result = future.result()
      if result:
        print(f"参数 {params} 返回结果: {result}")
    except Exception as e:
      print(f"处理结果时发生异常 {params}: {str(e)}")